package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author chau
 */
public class MenuObject {
    
    private static final int MENU_WIDTH = 128;
    private static final int DISTANCE_BETWEEN_LINE = 20;
    private int menuHeight;
    public int menuLength;
    private int menuSelectedIndex;
    private int topMenu;
    private int leftMenu;
    private boolean isShow;
    private String[] menuList;
    private Image menuSelectedImage;
    
    public MenuObject() {
        initImage();
    }
    
    private void initImage() {
        try {
            Image menuImagePiece = Utils.loadImage("sub_menu_color.png");
            menuSelectedImage = Utils.creatImageFromPiece(menuImagePiece, 1, MENU_WIDTH - 1);
        } catch (IOException ex) {
        }
    }
    
    public void setVisible(boolean isShow) {
        this.isShow = isShow;
        menuSelectedIndex = 0;
//        Debug.d("length :" + menuLength);
    }
    
    public String[] getList() {
        return this.menuList;
    }
    
    public void setList(String[] menuList) {
        this.menuList = menuList;
        if (menuList == null) {
            menuLength = 0;
            return;
        }
        menuLength = menuList.length;
        menuHeight = (menuLength + 1) * DISTANCE_BETWEEN_LINE + 2;
        menuSelectedIndex = 0;
        Debug.d("menu  :" + menuLength);
        
    }
    
    public void setList(String[] menuList, int length) {
        this.menuList = menuList;
        if (menuList == null) {
            menuLength = 0;
            return;
        }
        menuLength = length;
        menuHeight = (menuLength + 1) * DISTANCE_BETWEEN_LINE + 2;
        menuSelectedIndex = 0;
    }
    
    public void setList(int index, String str) {
        this.menuList[index] = str;
    }
    
    public int getSelectedIndex() {
        return this.menuSelectedIndex;
    }
    
    public void paint(Graphics g, int leftMenu, int bottom) {
        if (!isShow || menuLength == 0 || menuList == null) {
            isShow = false;
            return;
        }
        this.topMenu = bottom - menuHeight;
//        this.topMenu = topMenu;
        this.leftMenu = leftMenu;
        g.setColor(0xffffff);
        g.drawRoundRect(leftMenu, topMenu, MENU_WIDTH, menuHeight, 7, 7);
        g.setColor(0x343941);
        g.fillRoundRect(leftMenu + 1, topMenu + 1, MENU_WIDTH - 1, menuHeight - 1, 7, 7);
        for (int i = 0; i < menuLength; i++) {
            if (i == menuSelectedIndex) {
                g.drawImage(menuSelectedImage, leftMenu + 1, topMenu + menuSelectedIndex * DISTANCE_BETWEEN_LINE + 7, 0);
            }
            PiPoDesigner.drawCenterString(g, menuList[i], PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_WHITE, leftMenu + 60, topMenu + i * DISTANCE_BETWEEN_LINE + 10);
        }
    }
    
    public void doKeyUp() {
        menuSelectedIndex--;
        if (menuSelectedIndex < 0) {
            menuSelectedIndex = menuLength - 1;
        }
    }
    
    public void doKeyDown() {
        menuSelectedIndex = (++menuSelectedIndex) % menuLength;
    }
    
    public boolean isTouch(int x, int y) {
        
        for (int i = 0; i < menuLength; i++) {
            if (Utils.isTouch(leftMenu, topMenu + i * DISTANCE_BETWEEN_LINE, MENU_WIDTH, menuSelectedImage.getHeight() + 10, x, y)) {
                menuSelectedIndex = i;
                return true;
            }
        }
        return false;
    }
}
